// pages/authorization/authorization.js
let app = getApp()
let request = require("../../utils/request.js") //封装的request请求
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    agreementVip: true,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    this.setData({
          state:options.state,
        })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 授权登陆获取用户信息
   */
  getUserProfile(e) {
    wx.getUserProfile({
      desc: '用于完善会员资料', 
      success: (res) => {
        console.log('获取用户信息：', res)
        this.setData({ userInfo: res.userInfo })
        wx.setStorageSync('userInfo', res.userInfo)
        wx.login({
          success: res => {
            this.getToken(res.code, e.currentTarget.dataset.url == null ? e.detail : e.currentTarget.dataset.url);
            let state = this.data.state;
            console.log(state);
            if ( state == 1 ) {
              wx.reLaunch({
                url: '../appointNorthland/appointNorthland',
              })
            }else {
              wx.reLaunch({
                url: '../getParkingTicket/getParkingTicket',
              })
            }

          }
        });
      }
    })
    
  },

  getPhoneNumber: function (e) {
    var that = this;
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      let encryptedData = e.detail.encryptedData;
      let iv = e.detail.iv;
      wx.login({
        success(res) {
          let url = '/customer/info/bindMobilePhone'
          let params = {
            code: res.code,
            encryptedData: encryptedData,
            iv: iv
          }
          request.http(url, 'GET', params)
            .then(res => {
              console.log(res);
              if (res.data.code == 200) {
                Toast.success('绑定成功');
                that.setData({
                  'user.mobilePhone': res.data.data,
                  showBindPhone: false
                })
              }
            })
        }
      })
    }
  },

    /**
   * 获取Token
   * @param {String} code 微信返回的用户code
   */
  getToken(code, src) {
    let userInfo = this.data.userInfo;
    let params = {
      avatar: userInfo.avatarUrl,
      code,
      nickname: userInfo.nickName,
      sex: userInfo.gender == 0 ? 'UNKNOWN' : userInfo.gender == 1 ? 'MEN' : 'WOMEN',
    }
    request.http(
      '/customer/info/authorize', 
      'POST', 
      params
    ).then(res => {
      console.log('获取token：0000000000000000000000000', res)
      var data = res.data
      if (data.code == 200) {
        wx.setStorageSync('token', data.data.token)
        wx.setStorageSync('customerId', data.data.customerId)
        this.setData({
          token: data.data.token,
          userInfo: wx.getStorageSync('userInfo'),
          //应该会出  地址重复问题
          // websocketUrl: app.globalData.websocketUrl + 'customer/' + data.data.customerId
        })
      } else {
        wx.removeStorageSync('userInfo')
      }
    })
  },
  // 取消登陆
  cancelLogin() {
    let _this = this;
    wx.showModal({
      title: '警告',
      content: '关闭后，请重新授权',
      confirmText: '关闭',
      cancelText:"取消",
      success: function(res) {
        if (res.confirm) {
          console.log('点击了关闭按钮')
          _this.setData({
            orderState:false
          })
        } else {
          _this.setData({
            orderState:false
          })
          console.log('点击了取消按钮')
        }
      }
    });
  },



/**
* 用户协议弹窗
*/
showAgreement(e) {
  let type = e.currentTarget.dataset.type
  this.setData({
    agreementShow: true,
    agreementType: type
  })
},
/**
 * 关闭用户协议弹窗
 */
showAgreementClose() {
  this.setData({
    agreementShow: false
  })
},
/**
 * 同意协议按钮事件
 */
showAgreementCloseEvent(e, a) {
  let agreement = e.detail
  this.setData({
    agreementVip: agreement
  })
},

})